#!/bin/bash

# gRPC连接配置修复测试脚本 (Linux版本)
# 验证too_many_pings问题的修复

echo "🔧 gRPC连接配置修复测试脚本"
echo "=================================="

echo "📋 测试说明:"
echo "  本脚本将测试修复后的gRPC连接配置"
echo "  验证是否解决了too_many_pings问题"
echo

echo "🚀 开始测试..."
echo

echo "1. 启动单个服务实例进行连接测试"
echo

# 启动Gate服务实例
echo "启动Gate服务实例 (serverId=1001, gRPC端口=14000, Web端口=4000)"
cd gate && nohup mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dserver.id=1001 -Dgrpc.server.port=14000 -Dserver.port=4000" > ../gate-test.log 2>&1 &
GATE_PID=$!
cd ..

sleep 15

echo
echo "2. 等待服务启动完成..."
echo

sleep 20

echo
echo "3. 验证服务健康状态"
echo

echo "检查Gate服务健康状态:"
curl -s http://localhost:4000/actuator/health
echo

echo
echo "4. 测试gRPC连接配置"
echo

echo "测试Gate服务gRPC连接 (使用优化配置):"
grpcurl -plaintext localhost:14000 list
echo

echo
echo "5. 监控连接日志"
echo

echo "💡 观察要点:"
echo "  - 检查是否还有too_many_pings错误"
echo "  - 验证keepalive配置是否生效"
echo "  - 确认连接稳定性"
echo

echo "📊 连接配置详情:"
echo "  - keepAliveTime: 60秒 (避免频繁ping)"
echo "  - keepAliveTimeout: 10秒 (合理的超时时间)"
echo "  - keepAliveWithoutCalls: false (关闭无调用时的keepalive)"
echo "  - maxInboundMessageSize: 1MB"
echo

echo "🔍 查看实时日志:"
echo "tail -f gate-test.log"
echo

echo "📊 进程信息:"
echo "Gate实例 PID: $GATE_PID"
echo

echo "⏰ 测试持续时间: 建议运行5-10分钟"
echo "  观察期间是否出现too_many_pings错误"
echo

echo "🎯 预期结果:"
echo "  - 不再出现too_many_pings错误"
echo "  - 连接保持稳定"
echo "  - keepalive ping间隔合理"
echo

echo "📝 测试完成后:"
echo "  1. 观察gate-test.log日志文件"
echo "  2. 检查是否有连接错误"
echo "  3. 验证连接配置是否生效"
echo

echo "🛑 停止测试服务:"
echo "kill $GATE_PID"
echo

echo "📋 常用监控命令:"
echo "  # 查看实时日志"
echo "  tail -f gate-test.log"
echo
echo "  # 查看gRPC连接相关日志"
echo "  grep -i 'grpc\|ping\|connection' gate-test.log"
echo
echo "  # 查看错误日志"
echo "  grep -i 'error\|exception' gate-test.log"
echo 